/*  
 *  Copyright (c) 2008 Adrian Necimal <necimal@gmail.com>
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

const Cc = Components.classes;
const Ci = Components.interfaces;

Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

function SongbirdRecommendations() {
  this.wrappedJSObject = this;
};

SongbirdRecommendations.prototype = {
  classDescription: "Songbird Recommendations Javascript XPCOM Component",
  classID: Components.ID("{DB48C84A-0A92-40e3-8ACC-C21CC6D2D873}"),
  contractID: "@necimal.com/recommendations;1",
  
  QueryInterface: XPCOMUtils.generateQI([]),
  
  query: function(artist) {
    var result = { };
    result.queryUrl = "http://ws.audioscrobbler.com/2.0/artist/info.xml?artist=" + this.escapeUrl(artist);
    
    // make a request to Last.fm
    var req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
    
    req.open("GET", result.queryUrl, false);
    req.send(null);
    if (!req.responseXML) {
      return null;
    }
    
    
    // get some basic details
    var artistNode = req.responseXML.getElementsByTagName("artist").item(0);
    result.name = artistNode.getElementsByTagName("name").item(0).firstChild.nodeValue;
    result.url = artistNode.getElementsByTagName("url").item(0).firstChild.nodeValue;
    
    var imageNode = artistNode.getElementsByTagName("image").item(1);
    if (imageNode.firstChild) {
      result.image = imageNode.firstChild.nodeValue;
    } else { result.image = ""; }
    
    // get a bio
    var summaryNode = artistNode.getElementsByTagName("summary").item(0);
    if (summaryNode.firstChild) {
      result.summary = this.stripHtml(summaryNode.firstChild.nodeValue);
    } else { result.summary = ""; }
    
    // get similar artists
    var similarNode = artistNode.getElementsByTagName("similar").item(0);
    var similarArtistNodes = similarNode.getElementsByTagName("artist");
    result.similar = new Array(similarArtistNodes.length);
    
    for (i=0; i < similarArtistNodes.length; i++) {
      var similarArtist = similarArtistNodes.item(i);
      
      // find some details on each similar artist
      result.similar[i] = { };
      result.similar[i].name = similarArtist.getElementsByTagName("name").item(0).firstChild.nodeValue;
      result.similar[i].url = similarArtist.getElementsByTagName("url").item(0).firstChild.nodeValue;
      
      var similarArtistImage = similarArtist.getElementsByTagName("image").item(0);
      if (similarArtistImage.firstChild) {
        result.similar[i].image = similarArtistImage.firstChild.nodeValue
      } else { result.similar[i].image = ""; }
    }
    
    // finished getting artist info
    return result;
  },
  
  escapeUrl: function(url) {
    if (!url) { return ""; }
    else {
      url = url.replace(/&/g, "%26");
      return url;
    }
  },
  
  stripHtml: function(text) {
    text = text.replace(/(<([^>]+)>)/ig, "");
    text = text.replace(/&quot;/ig, "\"");
    text = text.replace(/&amp;/ig, "&");
    return text;
  }
};

var components = [SongbirdRecommendations];
function NSGetModule(compMgr, fileSpec) {
  return XPCOMUtils.generateModule(components);
}
